Adioso Agent Refactoring

Rishabh Jain 11 年 前
コミット
949fc48c89
共有4 個のファイルを変更した12 個の追加10 個の削除を含む
  1. 8 6
      app/models/agents/travel_agent.rb
  2. 0 0
      spec/data_fixtures/adioso_fare.json
  3. 0 0
      spec/data_fixtures/adioso_parse.json
  4. 4 4
      spec/models/agents/travel_agent_spec.rb

+ 8 - 6
app/models/agents/travel_agent.rb

@@ -1,12 +1,12 @@
1 1
 module Agents
2
-  class TravelAgent < Agent
2
+  class AdiosoAgent < Agent
3 3
 
4 4
   cannot_receive_events!
5 5
 
6 6
 	default_schedule "every_1d"
7 7
 
8 8
   description <<-MD
9
-		Travel Agent will tell you about the minimum airline prices between a pair of cities, and between a certain period of time.
9
+		Adioso Agent will tell you about the minimum airline prices between a pair of cities, and between a certain period of time.
10 10
     Currency is USD, Please make sure difference between `start_date` and `end_date` is less than 150 days. You will need to contact [Adioso](http://adioso.com/)
11 11
 		for `username` and `password`.
12 12
   MD
@@ -30,7 +30,7 @@ module Agents
30 30
         :to         => "Chicago",
31 31
         :username   => "xx",
32 32
         :password   => "xx",
33
-				:expected_update_period_in_days => "2"
33
+				:expected_update_period_in_days => "1"
34 34
       }
35 35
     end
36 36
 
@@ -39,17 +39,19 @@ module Agents
39 39
     end
40 40
 
41 41
     def validate_options
42
-			errors.add(:base, "All fields are required") unless options[:start_date].present? && options[:end_date].present? && options[:from].present? && options[:to].present? && options[:username].present? && options[:password].present? && options[:expected_update_period_in_days].present?
42
+			unless %w[start_date end_date from to username password expected_update_period_in_days].all? { |field| options[field.to_sym].present? }
43
+				errors.add(:base, "All fields are required")
44
+			end
43 45
 		end
44 46
 
45
-    def datetounixtime(date)
47
+    def date_to_unix_epoch(date)
46 48
       date.to_time.to_i
47 49
     end
48 50
 
49 51
     def check
50 52
       auth_options = {:basic_auth => {:username =>options[:username], :password=>options[:password]}}
51 53
       parse_response = HTTParty.get "http://api.adioso.com/v2/search/parse?q=#{URI.encode(options[:from])}+to+#{URI.encode(options[:to])}", auth_options
52
-      fare_request = parse_response["search_url"].gsub /(end=)(\d*)([^\d]*)(\d*)/, "\\1#{datetounixtime(options[:end_date])}\\3#{datetounixtime(options[:start_date])}"
54
+      fare_request = parse_response["search_url"].gsub /(end=)(\d*)([^\d]*)(\d*)/, "\\1#{date_to_unix_epoch(options[:end_date])}\\3#{date_to_unix_epoch(options[:start_date])}"
53 55
       fare = HTTParty.get fare_request, auth_options
54 56
 			unless fare["warnings"]
55 57
 				event = fare["results"].min {|a,b| a["cost"] <=> b["cost"]}

spec/data_fixtures/travel_fare.json → spec/data_fixtures/adioso_fare.json


spec/data_fixtures/travel_parse.json → spec/data_fixtures/adioso_parse.json


+ 4 - 4
spec/models/agents/travel_agent_spec.rb

@@ -1,9 +1,9 @@
1 1
 require 'spec_helper'
2 2
 
3
-describe Agents::TravelAgent do
3
+describe Agents::AdiosoAgent do
4 4
 	before do
5
-		stub_request(:get, /parse/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/travel_parse.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
6
-		stub_request(:get, /fares/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/travel_fare.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
5
+		stub_request(:get, /parse/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/adioso_parse.json")), :status => 200, :headers => {"Content-Type" => "text/json"})
6
+		stub_request(:get, /fares/).to_return(:body => File.read(Rails.root.join("spec/data_fixtures/adioso_fare.json")),  :status => 200, :headers => {"Content-Type" => "text/json"})
7 7
 		@valid_params = {
8 8
 											:start_date => "June 25 2013",
9 9
 											:end_date   => "July 15 2013",
@@ -14,7 +14,7 @@ describe Agents::TravelAgent do
14 14
 											:expected_update_period_in_days => "2"
15 15
 										}
16 16
 
17
-		@checker = Agents::TravelAgent.new(:name => "somename", :options => @valid_params)
17
+		@checker = Agents::AdiosoAgent.new(:name => "somename", :options => @valid_params)
18 18
 		@checker.user = users(:jane)
19 19
 		@checker.save!
20 20
 	end